<?php

/*
	info: 图片展示
*/

define('NOT_HTMLPAGE', TRUE);
define('NOT_MYSQL', TRUE);
define('NOT_LOGIN', TRUE);
require dirname(__FILE__).'/include/common.inc.php';

$webrequest['f'] = isset($webrequest['f']) ? $webrequest['f'] : '';	//文件绝对路径加密
$webrequest['w'] = isset($webrequest['w']) ? min(9999, intval($webrequest['w'])) : 0;	//图片宽度
$webrequest['h'] = isset($webrequest['h']) ? min(9999, intval($webrequest['h'])) : 0;	//图片高度

if ($webrequest['f']=='')
{
	showimage_Error();
}

$curabsfile = WEBFILEPATH.'/files/'.$webrequest['f'];
if ($curabsfile=='')
{
	showimage_Error();
}
if (global_IsImageFile($curabsfile) && ($webrequest['w']>0 || $webrequest['h']>0))
{
	include_once WEBFILEPATH.'/include/image.class.php';
	$curusefile = $curabsfile;
	$image = new Image($curusefile);
	if ($webrequest['w']>0 || $webrequest['h']>0)
	{
		$setcuttype = 5;
		if ($webrequest['w']<=0)
		{
			$setcuttype = 147;
			$webrequest['w'] = $webrequest['h'];
		}
		elseif ($webrequest['h']<=0)
		{
			$setcuttype = 123;
			$webrequest['h'] = $webrequest['w'];
		}
		$image->SetResize($webrequest['w'], $webrequest['h'], $setcuttype, (($webrequest['w']>500 || $webrequest['h']>500) ? 1 : 0));
		$image->Resize();
	}
	$image->DoInit();
	$image->ShowFile(strtoupper(global_FileExt($curusefile)));
	$image->Destroy();
	if (!empty($image->errorMsg))
	{
		showimage_Error();
	}
	exit();
}
else
{
	$curfiletype = global_FileExt($curabsfile);
	$curfilesize = filesize($curabsfile);
	$curfilename = basename($curabsfile);
	switch ($curfiletype)
	{
		case 'gif':
			header("Content-type: image/gif");
			break;
		case 'jpg':
		case 'jpeg':
			header("Content-type: image/jpeg");
			break;
		case 'png':
			header("Content-type: image/png");
			break;
		case 'bmp':
			header("Content-type: image/bmp");
			break;
		default:
			showimage_Error();
	}
	header("Accept-Ranges: bytes");
	header("Content-Length: {$curfilesize}");
	header("Content-Disposition: inline; filename=\"{$curfilename}\"");
	$fp = fopen($curabsfile, 'r');
	$buffer_size = 10240;
	$cur_pos = 0;
	while (!feof($fp) && $curfilesize-$cur_pos>$buffer_size)
	{
		$buffer = fread($fp, $buffer_size);
		echo $buffer;
		$cur_pos += $buffer_size;
	}
	$buffer = fread($fp, $curfilesize-$cur_pos);
	echo $buffer;
	fclose($fp);
}

exit();


function showimage_Error()
{
	header("HTTP/1.0 404 Not Found");
	exit();
}
